package cn.kshost.zqyg.user.controller;

import cn.kshost.zqyg.model.dto.h5.UserLoginDto;
import cn.kshost.zqyg.model.dto.h5.UserRegisterDto;
import cn.kshost.zqyg.model.entity.user.UserInfo;
import cn.kshost.zqyg.model.vo.common.Result;
import cn.kshost.zqyg.model.vo.common.ResultCodeEnum;
import cn.kshost.zqyg.model.vo.h5.UserInfoVo;
import cn.kshost.zqyg.user.service.UserService;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "会员接口")
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/userInfo/register")
    @Operation(summary = "会员注册")
    public Result register(@RequestBody UserRegisterDto userRegisterDto) {

        userService.register(userRegisterDto );

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/userInfo/login")
    @Operation(summary = "会员登录")
    public  Result login(@RequestBody UserLoginDto userLoginDto, HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        String token =  userService.login(userLoginDto,ip);
       return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("/userInfo/auth/getCurrentUserInfo")
    @Operation(summary = "获取当前登陆了用户信息")
    public Result getCurrentUserInfo( HttpServletRequest request) {
        String token = request.getHeader("token");
        UserInfoVo userInfoVo =  userService.getCurrentUserInfo(token);
        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }

}
